import { useUserStoreWithOut } from '@/stores/modules/user'
import { SystemRoleCodeEnum } from '@/utils/constants'

// 验证用户拥有指定权限
export const hasPermi = (perm: string) => {
  const all_permission = '*:*:*'
  const permissions = useUserStoreWithOut().permissions
  if (!permissions?.length) {
    return false
  }
  return permissions.some((permission) => {
    return permission === all_permission || permission === perm
  })
}

// 验证用户拥有指定角色
export const hasRole = (role: string) => {
  const roles = useUserStoreWithOut().roles
  if (!roles?.length) {
    return false
  }
  return roles.some((r) => {
    return r === SystemRoleCodeEnum.Super_Admin || r === role
  })
}

// 验证用户拥有任一指定权限
export const hasAnyPermi = (perms: string[]) => {
  if (!perms?.length) {
    return true
  }
  return perms.some((perm) => hasPermi(perm))
}

// 验证用户拥有全部指定权限
export const hasAllPermi = (perms: string[]) => {
  if (!perms?.length) {
    return true
  }
  return perms.every((perm) => hasPermi(perm))
}

// 验证用户拥有任一指定角色
export const hasAnyRole = (roles: string[]) => {
  if (!roles?.length) {
    return true
  }
  return roles.some((role) => hasRole(role))
}

// 验证用户拥有全部指定角色
export const hasAllRole = (roles: string[]) => {
  if (!roles?.length) {
    return true
  }
  return roles.every((role) => hasRole(role))
}

export const isSuperAdmin = () => {
  return hasRole(SystemRoleCodeEnum.Super_Admin)
}

export const isAdmin = () => {
  return hasAnyRole([SystemRoleCodeEnum.Super_Admin, SystemRoleCodeEnum.Tenant_Admin])
}
